package com.qifei.xmlg_backend.controller;

import com.qifei.xmlg_backend.entity.Dept;
import com.qifei.xmlg_backend.entity.Result;
import com.qifei.xmlg_backend.service.IDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author qifei
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/depts")
@Tag(name="部门管理")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @GetMapping()
    @Operation(summary = "获取部门列表",description = "获取部门全部列表")
    public Result<List> list(){
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }

    @PostMapping()
    @Operation(summary = "添加部门",description = "")
    public Result<Void> add(@RequestBody Dept dept){
        boolean success = deptService.save(dept);
        if (success){
            return Result.success();
        }else{
            return Result.error("添加部门失败");
        }
    }


    @PutMapping()
    @Operation(summary = "更新部门",description = "")
    public Result<Void> update(@RequestBody Dept dept){
        boolean success = deptService.updateById(dept);
        if (success){
            return Result.success();
        }else{
            return Result.error("更新部门失败");
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门",description = "")
    public Result<Void> delete(@PathVariable int id){
        boolean success = deptService.removeById(id);
        if (success){
            return Result.success();
        }else{
            return Result.error("删除部门失败");
        }
    }

}
